package com.agv.core.dao.rpt;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.agv.core.dao.dataExtractor.DataModelRSExtractor;
import com.agv.core.dao.dataExtractor.GenericRowMapper;
import com.agv.core.exception.AgvException;
import com.agv.core.objectDomain.DataModel;

public class RptDaoImpl implements IRptDao {

	private final String GET_REPORT = "select * from adm.adm003_cfg_reports where nb_report = ?";

	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DataModel getDataModel(String nbReport) throws AgvException {
		try {
			DataModel dataModel = (DataModel) jdbcTemplate.query(GET_REPORT,
					new Object[] { nbReport }, new DataModelRSExtractor());
			List<Map<String, String>> data = (List<Map<String, String>>) jdbcTemplate
					.query(dataModel.getNbQuery(), new GenericRowMapper());
			dataModel.setData(data);
			return dataModel;
		} catch (Exception e) {
			throw new AgvException(e);
		}
	}

}
